<%-- 
    Document   : newcontact
    Created on : 26 déc. 2013, 20:35:35
    Author     : root
--%>

<%@page import="presenter.Presenter"%>
<%@page import="java.util.Map.Entry"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% String error = "";
    String name = "";
    String mail = "";
    String sorry = "";
    HttpSession thisSession = (HttpSession) request.getAttribute("thisSession");
    ArrayList<String> contacts = (ArrayList<String>) thisSession.getAttribute("contacts");
    String msgFile = "";
    Map<String, String> waitingFile = (HashMap<String, String>) request.getAttribute("waitingFile");
    if (waitingFile.size() > 0) {
        msgFile = msgFile + "(" + waitingFile.size() + ")";

    }
    String demande = "";
    ArrayList<String> demandeContact = (ArrayList<String>) thisSession.getAttribute("demandeContact");
    if (demandeContact.size() != 0) {
        boolean accept = true;
        int notif = demandeContact.size();
        if (notif != 0) {
            demande = " (" + notif + ")";
        }
    }
    Map<String, String> e = (Map<String, String>) request.getAttribute("erreur");
    Map<String, String> results = (Map<String, String>) request.getAttribute("results");
    if (e.containsKey("idError")) {
        error = e.get("idError");
    }
    if (e.containsKey("idSorry")) {
        sorry = e.get("idSorry");
    }
    ArrayList<String> newMessage = (ArrayList<String>) thisSession.getAttribute("newMessage");
    String newMsg = "";
    if (newMessage.size() != 0) {
        newMsg = newMsg + " (" + newMessage.size() + ")";
    }

    if (demandeContact.size() != 0) {
        boolean accept = true;
        int notif = demandeContact.size();
        if (notif != 0) {
            demande = " (" + notif + ")";
        }
    }

    if (newMessage.size() != 0) {
        newMsg = newMsg + " (" + newMessage.size() + ")";
    }
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/NewContact.css"/>
        <title>MIS System</title>
    </head>
    <body>
        <header>
            <a href="RedirectConnected"><img src="Pictures/logo.png" class="logo" style="float: outside;"></a>
            <div class="zone">
                <div class="zone-right"></div>
                <div class="zone-left"></div>
                <div class="zone-bg">
                    <ul>
                        <li><a href="RedirectConnected"><span>Home <%= newMsg%></span></a></li>
                        <li class="current"><a href="NewContact.html"><span>Contact <%= demande%></span></a></li>
                        <li><a href="profile.html"><span>Profile</span></a></li>
                        <li><a href="File.html"><span>File <%= msgFile%></span></a></li>
                        <li><a href="Administrator.html"><span>Administration</span></a></li>
                        <li><a href="logout.html"><span>Logout</span></a></li>
                    </ul>
                </div>
            </div>
        </header>

        <div id="Contactlist">
            Your contact list:
            <form method="post" action="FileDownload">
                <select name="fileName" multiple>
                    <% for (String contact : contacts) {
                    %>
                    <option value="<%=Presenter.getTotalConversation((String) session.getAttribute("login"), contact)%>"><%=contact%></option>
                    <% }
                    %>
                </select><br>
                <input type="submit" value="History">
            </form>
        </div>

        <% if (demandeContact.size() != 0) {
        %> <div id="Contactrequest">
            <%
                out.write("You have " + demandeContact.size() + " contact request");
                String cont;
                for (int i = 0; i < demandeContact.size(); i++) {
                    cont = "" + demandeContact.get(i);
            %>
            <div id="NewContact">
                <form method="post" action="AcceptContact">
                    <div id="name"> <%= cont%> </div><input type="hidden" name="newcontact" value="<%= cont%>">
                    <input type="image" src="Pictures/add.jpeg" onclick="submit">
                </form>
                <form method="post" action="RefuseContact">
                    <input type="hidden" name="name" value="<%= cont%>">
                    <input type="submit" value="refuse">
                </form>
            </div><br>
        </div>
        <%
                }
            }%>

        <div id="Addcontact">
            <h3>Add a contact</h3>
            <div class="search">
                <div id="search">
                    <form method="post" action="AddContact">
                        <label for="idSearch">search contact</label><input type="text" name="research" id="idSearch"><em><%=error%></em><br>
                        By
                        <select name="type" id="idSearch">
                            <option value="login">Login</option>
                            <option value="name">Name</option>
                            <option value="mail">Mail</option>
                        </select>
                        <input type="submit" value="search">
                    </form>
                </div>
            </div>
            <div class="results">
                <h5>Results</h5>
                number of matches : <em><%= results.size()%> </em><em><%= sorry%></em>

                <% for (Entry<String, String> entry : results.entrySet()) {
                        name = entry.getKey();
                        mail = entry.getValue();
                %>

                <div class="matches">
                    <div id="name">Name : <%= name%></div> 
                    <div id="mail">Mail : <%= mail%></div>
                    <form method="post" action="Add">
                        <input type="hidden" name="name" value="<%= name%>">
                        <input type="image" src="Pictures/add.jpeg" onclick="submit">
                    </form>

                </div><br>	

                <%    }
                %>


            </div>
        </div>
    </body>
</html>
